package com.masterclient.app.businessmodule.minemodule.point.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;

import com.masterclient.app.R;
import com.eoner.baselib.utils.notify.RxBus;
import com.eoner.baselibrary.utils.ScreenUtils;

/**
 * 作者：sean
 * 创建时间：2017/8/31 18:38
 * 功能说明：
 */

public class PointExchangeRuleDialog extends Dialog {

    private Context mContext;
    RxBus rxBus;
    private WebView webView;
    private String url;

    public PointExchangeRuleDialog(@NonNull Context context,String url) {
        super(context,R.style.MyDialog);
        mContext = context;
        rxBus = RxBus.$();
        this.url = url;
    }


    View confirm_btn;
    ImageView iv_close;

    private String commission;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_point_exchang_rule);

        confirm_btn = findViewById(R.id.confirm_btn);
        iv_close = (ImageView) findViewById(R.id.iv_close);
        webView = (WebView) findViewById(R.id.webView);
        WebChromeClient wvcc = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        };

        WebSettings webSettings = webView.getSettings();

        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);

        //支持插件

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
//      String ua = webSettings.getUserAgentString();
//      webSettings.setUserAgentString(ua+"; shbn/"+ Utils.getVersionName());

        //缩放操作
        webSettings.setSupportZoom(false); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
        webSettings.setDefaultFixedFontSize(16);
        webView.setWebChromeClient(wvcc);
        webView.loadUrl(url);

        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        confirm_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }


    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        windowparams.height = (int) (ScreenUtils.getScreenHeight()*0.5);
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
}
